package cn.tedu.egmvc5.controller;

import cn.tedu.egmvc5.basc.response.JsonResult;
import cn.tedu.egmvc5.mapper.AddressMapper;
import cn.tedu.egmvc5.pojo.dto.AddressAddParam;
import cn.tedu.egmvc5.pojo.entity.Address;
import cn.tedu.egmvc5.pojo.vo.AddressInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/v1/address/")
public class AddressController {
    @Autowired
    public AddressMapper addressMapper;

    /**
     * 添加收货地址
     * @param addressAddParam
     * @return
     */
    @PostMapping("add")
    public JsonResult AddAddress(AddressAddParam addressAddParam){
        System.out.println("addressAddParam:"+addressAddParam);
        Address address = new Address();
        BeanUtils.copyProperties(addressAddParam,address);
        addressMapper.insertAddress(address);

        return JsonResult.success();
    }

    @GetMapping("list")
    public JsonResult addressInfoVOS(AddressInfoVO addressInfoVO){
        System.out.println("addressInfoVO:"+addressInfoVO);
        List<AddressInfoVO> addressInfoVOS = addressMapper.selectAddress();

        return JsonResult.success(addressInfoVOS);
    }

    @PostMapping("del")
    public JsonResult delectAddress(Integer id){
        System.out.println("id="+id);
        addressMapper.delectAddress(id);

        return JsonResult.success();
    }
}
